<template>
  <qx-table ref="DictTable" :option="formOption" :remoteData="remoteData">
    <template v-slot:menuLeft="{ selected }">
      <q-btn v-auth="['cfgDict:save']" color="primary" icon="add" unelevated @click="handleCreate">
        {{ $t('Add') }}
      </q-btn>
      <q-btn v-auth="['cfgDict:update']" :disable="isDisable(selected)" icon="mdi-square-edit-outline" outline
             text-color="primary" @click="handleUpdate(selected[0])">
        {{ $t('Edit') }}
      </q-btn>
      <q-btn v-auth="['cfgDict:delete']" :disable="isDisable(selected)" icon="delete" outline text-color="primary"
             @click="handleDelete(selected[0])">
        {{ $t('Del') }}
      </q-btn>
    </template>

    <template v-slot:dictTypeCode="{ row }">
      <q-link @click="handleData(row)">{{ row.dictTypeCode }}</q-link>
    </template>
  </qx-table>
  <qx-page v-model="pageVisible" :header="pageHeader" :menuBtn="false" classes="q-pb-md" fullHeight
           @beforeClose="pageClose">
    <DictDataCfg ref="DictData" :row-info="rowInfo"></DictDataCfg>
  </qx-page>

  <q-modal v-model="modalVisible" :header="formInfo.id ? $t('Edit') : $t('Add')" :width="500" btnAlign="center"
           position="right" @beforeClose="beforeClose" @confirm="submitForm">
    <qx-form ref="DictCfg" v-model="formInfo" :option="formOption"></qx-form>
  </q-modal>
</template>

<script setup>
import { deleteAPI, postAPI, putAPI } from 'src/api'
import { i18n, toI18nName } from 'boot/i18n'
import { yesOrNo } from 'src/enum'
import { createDialog, deepClone } from 'src/utils/utils'
import { computed, onMounted, ref } from 'vue'
import DictDataCfg from './DictDataCfg.vue'

const rowInfo = ref({})
const formInfo = ref({})
const DictCfg = ref(null)
const DictData = ref(null)
const searchForm = ref({})
const pageHeader = ref('')
const DictTable = ref(null)
const pageVisible = ref(false)
const modalVisible = ref(false)

const isDisable = computed(() => selected => !selected || selected.length !== 1 || selected[0].sysInner === '1')
const formOption = computed(() => {
  return {
    labelWidth: 100, searchLabelWidth: 70, selection: 'single', menuBtn: false, span: 22,
    column: [{
      label: `${i18n.t('SystemCore.DictType')}`,
      field: 'dictTypeCode',
      required: true,
      slot: true,
      minWidth: 80
    }, {
      label: `${i18n.t('SystemCore.DictNameENUS')}`,
      field: 'dictTypeNameEnUs',
      hide: true,
      minWidth: 80
    }, {
      label: `${i18n.t('SystemCore.DictName')}`,
      field: 'dictTypeNameZhCn',
      required: true,
      format: (_, row) => row[toI18nName('dictTypeNameZhCn')],
      minWidth: 80
    }, {
      label: `${i18n.t('BuiltIn')}`,
      field: 'sysInner',
      search: false,
      display: false,
      options: yesOrNo.value,
      align: 'center', useTag: true,
      minWidth: 80
    }, {
      label: `${i18n.t('CreationTime')}`,
      field: 'createdTime',
      display: false,
      minWidth: 80
    }, {
      label: `${i18n.t('Remarks')}`,
      field: 'remark',
      type: 'textarea',
      search: false,
      minWidth: 120,
      maxlength: 200
    }]
  }
})

const handleCreate = () => {
  formInfo.value = {}
  modalVisible.value = true
}

const handleUpdate = row => {
  formInfo.value = deepClone(row)
  modalVisible.value = true
}

const handleData = row => {
  rowInfo.value = deepClone(row)
  pageHeader.value = row[toI18nName('dictTypeNameZhCn')]
  pageVisible.value = true
}

const formatSearchForm = () => {
  return { ...searchForm.value }
}

const remoteData = ref({ dicUrl: '/config/api/cfgDictType/list' })
onMounted(() => startSearch())

const handleDelete = row => {
  createDialog({ msg: `${i18n.t('DelTips')}` }, () => {
    deleteAPI(`/config/api/cfgDictType/${row.id}`, {}, () => startSearch())
  }, () => {})
}

const beforeClose = () => {
  if (DictCfg.value) DictCfg.value.onReset()
  modalVisible.value = false
}

const pageClose = () => {
  if (DictData.value) DictData.value.clearSelection()
  pageVisible.value = false
}

const startSearch = (_, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
}

const submitForm = (mDone) => {
  DictCfg.value.validate((form, done) => {
    const API = form.id ? putAPI : postAPI
    API('/config/api/cfgDictType', form, () => {
      startSearch()
      done()
      if (mDone) mDone()
    }, () => {
      done()
      if (mDone) mDone(false)
    })
  }, () => {
    if (mDone) mDone(false)
  })
}

</script>